package com.hsxc.cdz.monitor.bean;

import java.io.Serializable;

import com.hsxc.cdz.base.util.BytesUtil;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountUtil;

/**
 * 
 * @author 刘力文
 * 报文的实体类
 *
 */
public class Message implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private String isOpen;        //启动字符
	private int contentLength;    //报文长度
	private String isRegister;    //是否注册
	private String response;      //响应码
    private String operator;       //运营商
	private String address;			//设备地址
	private String frame;			//命令字
	private String lineNum;			//流水号
	private byte[] content;			//消息体

	
	public String getIsOpen() {
		return isOpen;
	}

	public void setIsOpen(String isOpen) {
		this.isOpen = isOpen;
	}

	public int getContentLength() {
		return contentLength;
	}

	public void setContentLength(int contentLength) {
		this.contentLength = contentLength;
	}

	public String getIsRegister() {
		return isRegister;
	}

	public void setIsRegister(String isRegister) {
		this.isRegister = isRegister;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getFrame() {
		return frame;
	}

	public void setFrame(String frame) {
		this.frame = frame;
	}

	public String getLineNum() {
		return lineNum;
	}

	public void setLineNum(String lineNum) {
		this.lineNum = lineNum;
	}

	public byte[] getContent() {
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}

	public byte[] getByteFromMessage() {
		
		ByteBuf bytes = Unpooled.buffer(26+this.contentLength);
		byte[] result;
		byte[] temp;
		temp  = BytesUtil.strToBinBytes(this.getIsOpen(),2);
		bytes.writeBytes(temp);
		temp = BytesUtil.intToBytesDesc(this.getContentLength(), 2);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBinBytes(this.getIsRegister(), 1);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBinBytes(this.getResponse(), 3);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBcdBytes(this.getOperator(),2);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBcdBytes(this.getAddress(),8);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBinBytes(this.getFrame(),1);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBcdBytes(this.getLineNum(),7);
		bytes.writeBytes(temp);
		temp = this.getContent();
		if(temp!=null){
			bytes.writeBytes(temp);
			}
		
		
		result = new byte[bytes.readableBytes()];
		bytes.readBytes(result);
		ReferenceCountUtil.release(bytes);
		return result;
	}
	
	public  void getMessageFromByte(byte[] result1){
		if(result1 == null){
			return;
		}
		byte[] temp;
        temp = BytesUtil.getByte(0,2,result1);
        this.setIsOpen(BytesUtil.binBytesToStr(temp));
        
        temp = BytesUtil.getByte(2,2, result1);
        this.setContentLength(BytesUtil.bytesToIntDesc(temp));
        
        temp = BytesUtil.getByte(4,1,result1);
        this.setIsRegister(BytesUtil.binBytesToStr(temp));
        
        temp = BytesUtil.getByte(5,3,result1);
        this.setResponse(BytesUtil.binBytesToStr(temp));
        
        temp = BytesUtil.getByte(8, 2, result1);
        this.setOperator(BytesUtil.bcdBytesToStr(temp));
        
        temp = BytesUtil.getByte(10, 8, result1);
        this.setAddress(BytesUtil.bcdBytesToStr(temp));
        
        temp = BytesUtil.getByte(18, 1, result1);
        this.setFrame(BytesUtil.binBytesToStr(temp));
        
        temp = BytesUtil.getByte(19, 7, result1);
        this.setLineNum(BytesUtil.bcdBytesToStr(temp));
	}
	@Override
	public String toString() {
		return "message [isOpen=" + isOpen + ", contentLength=" + contentLength + ", isRegister=" + isRegister
				+ ", response=" + response + ", operator=" + operator + ", address=" + address + ", frame=" + frame
				+ ", lineNum=" + lineNum + "]";
	}

}
